package com.rfid.project.web;

import java.text.DateFormat;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.util.DateUtil;
import biz.appframe.PageJsonBean;

import com.rfid.project.exception.CannotUpdateException;
import com.rfid.project.model.ProjectMilestoneFinishApply;
import com.rfid.project.service.ProcessStatus;
import com.rfid.project.service.ProjectMilestoneFinishApplyService;
import com.rfid.project.model.People;
import com.rfid.project.service.PeopleService;
import com.rfid.project.model.ProjectMilestone;
import com.rfid.project.service.ProjectMilestoneService;
import com.rfid.project.util.StringUtil;

@Controller
public class ProjectMilestoneFinishApplyController {

	private static final Logger logger = LoggerFactory
			.getLogger(ProjectMilestoneFinishApplyController.class);

	@Autowired
	private ProjectMilestoneFinishApplyService projectMilestoneFinishApplyService;

	@Autowired
	private PeopleService peopleService;

	@Autowired
	private ProjectMilestoneService projectMilestoneService;

	@RequestMapping(value = "/projectMilestoneFinishApplymgr")
	public String projectMilestoneFinishApplymgr(HttpServletRequest request,
			HttpServletResponse response, Model model,
			@RequestParam(value = "projectMilestoneId", defaultValue = "-1") Long projectMilestoneId) {
		ProjectMilestone projectMilestone = this.projectMilestoneService.findOne(projectMilestoneId);
		model.addAttribute("projectMilestone", projectMilestone);
		return "projectMilestoneFinishApplymgr";
	}

	@RequestMapping(value = "/queryProjectMilestoneFinishApply")
	public String queryProjectMilestoneFinishApply(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "applyPersonId", defaultValue = "-1") Long applyPersonId,
			@RequestParam(value = "applyDescription", defaultValue = "") String applyDescription,
			@RequestParam(value = "applyTimeBegin", defaultValue = "") Date applyTimeBegin,
			@RequestParam(value = "applyTimeEnd", defaultValue = "") Date applyTimeEnd,
			@RequestParam(value = "checkPersonId", defaultValue = "-1") Long checkPersonId,
			@RequestParam(value = "checkComment", defaultValue = "") String checkComment,
			@RequestParam(value = "checkTimeBegin", defaultValue = "") Date checkTimeBegin,
			@RequestParam(value = "checkTimeEnd", defaultValue = "") Date checkTimeEnd,
			@RequestParam(value = "status", defaultValue = "") String status,
			@RequestParam(value = "projectMilestoneId", defaultValue = "-1") Long projectMilestoneId,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "20") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<ProjectMilestoneFinishApply> projectMilestoneFinishApplys = projectMilestoneFinishApplyService
				.findAll(applyPersonId, applyDescription, applyTimeBegin, applyTimeEnd, checkPersonId,
						checkComment, checkTimeBegin, checkTimeEnd, status,
						projectMilestoneId, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", projectMilestoneFinishApplys.getTotalElements());
		JSONArray models = new JSONArray();
		for (ProjectMilestoneFinishApply item : projectMilestoneFinishApplys) {
			JSONObject mod = new JSONObject();
			mod.put("id", item.getId());
			mod.put("statusCode", item.getStatus());
			mod.put("status", ProcessStatus.processStatusName(item.getStatus()));
			mod.put("applyTime", com.rfid.project.util.DateUtil.dateTheDayToString(item.getApplyTime()));
			mod.put("checkComment", StringUtil.getString(item.getCheckComment()));
			mod.put("checkTime", com.rfid.project.util.DateUtil.dateTheDayToString(item.getCheckTime()));
			mod.put("applyDescription", item.getApplyDescription());

			mod.put("applyPersonId", item.getApplyPerson().getId());
			mod.put("applyPersonName", item.getApplyPerson().getName());
			
			People people = item.getQualityCheckPerson();
			if (null == people) {
				mod.put("qualityCheckPersonId", -1);
				mod.put("qualityCheckPersonName", "");
			} else {
				mod.put("qualityCheckPersonId", people.getId());
				mod.put("qualityCheckPersonName", people.getName());
			}
			mod.put("qualityCheckDescription", StringUtil.getString(item.getQualityCheckDescription()));
			mod.put("qualityCheckDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getQualityCheckDate()));
			mod.put("qualityCheckInputTime", com.rfid.project.util.DateUtil.dateTheDayToString(item.getQualityCheckInputTime()));
			mod.put("qualityCheckStatus", item.getQualityCheckStatus().trim());
			
			People checkPerson = item.getCheckPerson();
			if (null == checkPerson) {
				mod.put("checkPersonId", -1);
				mod.put("checkPersonName", "");
			} else {
				mod.put("checkPersonId", checkPerson.getId());
				mod.put("checkPersonName", checkPerson.getName());
			}
			mod.put("projectMilestoneId", item.getProjectMilestone().getId());
			mod.put("projectMilestoneName", item.getProjectMilestone().getName());
			
			ProjectMilestone projectMilestone = item.getProjectMilestone();
			mod.put("qualityCheckSign", projectMilestone.getQualityCheckSign());

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}

	@RequestMapping(value = "/editProjectMilestoneFinishApply", method = RequestMethod.POST)
	public String editProjectMilestoneFinishApply(HttpServletRequest request,
			HttpServletResponse response,
			@Valid ProjectMilestoneFinishApply projectMilestoneFinishApply,
			Model model, RedirectAttributes redirectAttributes) {
		People applyPerson = projectMilestoneFinishApply.getApplyPerson();
		if(null == applyPerson || null == applyPerson.getId()){
			projectMilestoneFinishApply.setApplyPerson((People)request.getSession().getAttribute("currentUser"));
		}
		projectMilestoneFinishApplyService.saveProjectMilestoneFinishApply(projectMilestoneFinishApply);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/getProjectMilestoneFinishApply")
	public String getProjectMilestoneFinishApply(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "-1") Long id
			) {
		ProjectMilestoneFinishApply item = 
		projectMilestoneFinishApplyService
				.getProjectMilestoneFinishApply(id);
		
		JSONObject mod = new JSONObject();
		mod.put("id", item.getId());
		mod.put("status", item.getStatus());
		mod.put("applyTime", com.rfid.project.util.DateUtil
				.dateAllToString(item.getApplyTime()));
		mod.put("checkComment", item.getCheckComment());
		mod.put("checkTime", com.rfid.project.util.DateUtil
				.dateAllToString(item.getCheckTime()));
		mod.put("applyDescription", item.getApplyDescription());

		mod.put("applyPersonId", item.getApplyPerson().getId());
		mod.put("applyPersonName", item.getApplyPerson().getName());

		People people = item.getQualityCheckPerson();
		if (null == people) {
			mod.put("qualityCheckPersonId", -1);
			mod.put("qualityCheckPersonName", "");
		} else {
			mod.put("qualityCheckPersonId", people.getId());
			mod.put("qualityCheckPersonName", people.getName());
		}
		mod.put("qualityCheckDescription", StringUtil.getString(item.getQualityCheckDescription()));
		mod.put("qualityCheckDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getQualityCheckDate()));
		mod.put("qualityCheckInputTime", com.rfid.project.util.DateUtil.dateTheDayToString(item.getQualityCheckInputTime()));
		mod.put("qualityCheckStatus", item.getQualityCheckStatus().trim());
		
		People checkPerson = item.getCheckPerson();
		if (null == checkPerson) {
			mod.put("checkPersonId", -1);
			mod.put("checkPersonName", "");
		} else {
		}
		mod.put("projectMilestoneId", item.getProjectMilestone().getId());
		mod.put("projectMilestoneName", item.getProjectMilestone()
				.getName());
		mod.put("projectId", item.getProjectMilestone().getProject().getId());
		mod.put("projectName", item.getProjectMilestone().getProject().getName());
		
		ProjectMilestone projectMilestone = item.getProjectMilestone();
		mod.put("qualityCheckSign", projectMilestone.getQualityCheckSign());

		PageJsonBean.printObject(response, mod);
		return null;
	}
	
	@RequestMapping(value = "/qualityCheckProjectMilestoneFinishApply", method = RequestMethod.POST)
	public String qualityCheckProjectMilestoneFinishApply(HttpServletRequest request,
			HttpServletResponse response,
			@Valid ProjectMilestoneFinishApply projectMilestoneFinishApply,
			Model model, RedirectAttributes redirectAttributes) {
		People qualityCheckPerson = projectMilestoneFinishApply.getQualityCheckPerson();
		if (null == qualityCheckPerson || null == qualityCheckPerson.getId()) {
			People currentUser = (People) request.getSession().getAttribute("currentUser");
			projectMilestoneFinishApply.setQualityCheckPerson(currentUser);
		}
		JSONObject result = new JSONObject();
		boolean flag = true;
		try {
			projectMilestoneFinishApplyService.qualityCheckProjectMilestoneFinishApply(projectMilestoneFinishApply);
		} catch (CannotUpdateException e) {
			flag = false;
			result.put("msg", e.getMessage());
		}
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	@RequestMapping(value = "/checkProjectMilestoneFinishApply", method = RequestMethod.POST)
	public String checkProjectMilestoneFinishApply(HttpServletRequest request,
			HttpServletResponse response,
			@Valid ProjectMilestoneFinishApply projectMilestoneFinishApply,
			Model model, RedirectAttributes redirectAttributes) {
		People checkPerson = projectMilestoneFinishApply.getCheckPerson();
		if (null == checkPerson || null == checkPerson.getId()) {
			People currentUser = (People) request.getSession().getAttribute("currentUser");
			projectMilestoneFinishApply.setCheckPerson(currentUser);
		}
		JSONObject result = new JSONObject();
		boolean flag = true;
		try {
			projectMilestoneFinishApplyService.checkProjectMilestoneFinishApply(projectMilestoneFinishApply);
		} catch (CannotUpdateException e) {
			flag = false;
			result.put("msg", e.getMessage());
		}
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@RequestMapping(value = "/delProjectMilestoneFinishApply")
	public String delProjectMilestoneFinishApply(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.projectMilestoneFinishApplyService.deleteProjectMilestoneFinishApply(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	@RequestMapping(value = "/revocationFinishApplyProjectMilestoneFinishApply")
	public String revocationFinishApplyProjectMilestoneFinishApply(HttpServletRequest request,
			HttpServletResponse response,
			@Valid ProjectMilestoneFinishApply projectMilestoneFinishApply) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		try {
			this.projectMilestoneFinishApplyService.revocationFinishApplyProjectMilestoneFinishApply(projectMilestoneFinishApply);
		} catch (CannotUpdateException e) {
			flag = false;
			result.put("msg", e.getMessage());
		}
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
